package com.demo.day528;


import java.text.SimpleDateFormat;
import java.util.ArrayList;

import java.util.Date;
import java.util.Scanner;

/**
@Auther: MFM
@Date: 2021/5/28 - 05 - 28 - 19:05
@version: 1.0
*/

public class ATM{

    int index = -1;

    int i = 0;

    ArrayList<AddAction> list = new ArrayList<>();

    User[] userArrays = {
            new User("root", "123456", 10000),
            new User("zhangsan","112233",1000),
            new User("lisi","112233",1000),
            new User("wangwu","112233",1000),
            new User("reet","112233",1000)

    };

    public boolean login(Scanner scanner){
        for (int i = 2; i > -1; i--) {
            System.out.println("请输入银行账号");
            String UserID = scanner.next();
            System.out.println("请输入银行密码");
            String PassWord = scanner.next();

            User user1 = new User(UserID,PassWord,1);

            for (int j = 0; j < userArrays.length; j++) {
                if (userArrays[j].getUserID().equals(user1.getUserID()) && userArrays[j].getPassWord().equals(user1.getPassWord())){
                    index = j ;
                    break;
                }
            }

            if (index == -1){
                if (i==0){
                    System.out.println("三次输入错误，账号冻结！");
                    return false;
                }else{
                    System.out.println("账号或密码错误！你还有"+i+"次机会！");
                }
            }else{
                if (UserID.equals(userArrays[index].getUserID()) && PassWord.equals(userArrays[index].getPassWord())){
                    return true;
                }
            }
        }
        return false;
    }

    public void showMoney(){
        System.out.println(userArrays[index].getMoney());
    }

    public void Withdraw(Scanner scanner){
        System.out.println("请输入要取款的金额：");
        int money = scanner.nextInt();
        if (userArrays[index].getMoney()>=money){
            userArrays[index].setMoney(userArrays[index].getMoney()-money);
            getMoney(money);
            getTime();
        }else{
            System.out.println("对不起，你没有这么多钱！！！！（嘲笑）");
        }
    }

    public void Deposit(Scanner scanner){
        System.out.println("请输入要存款的金额：");
        int money = scanner.nextInt();
        userArrays[index].setMoney(userArrays[index].getMoney()+money);
        getMoney(money);
        getTime();
    }

    public void changePassWord(Scanner scanner){
        System.out.println("请输入原密码：");
        String OddPassWord = scanner.nextLine();
        if (OddPassWord.equals(userArrays[index].getPassWord())){
            System.out.println("请输入新密码：");
            String NewPassWord = scanner.nextLine();
            userArrays[index].setPassWord(NewPassWord);
            System.out.println("密码修改成功！");
        }else{
            System.out.println("原密码输入错误，即将返回菜单栏！");
        }
    }

    public void exit(){
        System.exit(0);
    }

    public void turnMoney(Scanner scanner) {
        System.out.println("请输入转账的用户名");
        String UserID = scanner.next();
        User user1 = new User(UserID,"1",1);
        if (user1.getUserID().equals(userArrays[index].getUserID())){
            System.out.println("你无法给自己转账");
        }else{
            for (int j = 0; j < userArrays.length; j++) {
                if (userArrays[j].getUserID().equals(user1.getUserID())){
                    System.out.println("请输入要转账的金额：");
                    int money = scanner.nextInt();
                    if (userArrays[index].getMoney()>=money){
                        userArrays[index].setMoney(userArrays[index].getMoney()-money);
                        int a = index;
                        for (int k = 0; k < userArrays.length; k++) {
                            if (userArrays[k].getUserID().equals(UserID)){
                                index = k ;
                            }
                        }
                        userArrays[index].setMoney(userArrays[index].getMoney()+money);
                        index = a;
                        getMoney(money);
                        getTime();
                    }else{
                        System.out.println("对不起，你没有这么多钱！！！！（嘲笑）");
                    }
                    break;
                }
            }
        }

    }

    public void ChangeUser(Scanner scanner){
        login(scanner);
    }

    public void TradeInformation(){
        System.out.println("===========交易信息==============");
        System.out.println("交易编号\t"+"交易金额\t"+"交易类型\t\t" +"交易时间");
        for (int j = 0; j < list.size()-1; j++) {
            if(list.get(j).getMoney()!=0){
                System.out.println((j+1)+"\t\t"+list.get(j).getMoney()+"\t\t"+list.get(j).getType()+"\t\t"+list.get(j).getTime());
            }
        }
        System.out.println("===========交易信息==============");

    }


    public void getType(String choice) {
        list.add(i,new AddAction());
        list.get(i).setType("");
        switch (choice) {
            case "2":
                list.get(i).setType("取款");
                break;
            case "3":
                list.get(i).setType("存款");
                break;
            case "4":
                list.get(i).setType("转账");
                break;
        }
    }

    public void getMoney(int money){
        list.get(i).setMoney(money);
    }

    public void getTime(){
        list.get(i).setTime("");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = new Date();
        String value = format.format(date);
        list.get(i).setTime(value);
        i++;
    }



}
